package com.yxkj.syh.app.huarong.activities.user;


import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.syh.app.basic.BR;
import com.syh.app.basic.base.BaseFragment;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Loog;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.AppointmentListAdp;
import com.yxkj.syh.app.huarong.bean.ReportResponse;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.FragmentUserBinding;
import com.yxkj.syh.app.huarong.util.TextViewUtil;

/**
 * 用户 个人
 */
public class UserFragment extends BaseFragment<FragmentUserBinding, UserVM> {

    private int NUMBER_WIDTH_SPACE;

    private AppointmentListAdp mAppointmentListAdp;

    public UserFragment() {
        // Required empty public constructor
    }

    public static UserFragment newInstance() {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.userVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView() {

        NUMBER_WIDTH_SPACE = (AppUtil.getScreenWidths(mActivity) - ComUtil.Dp2Px(mActivity, 50) - 2) / 3;

        mAppointmentListAdp = new AppointmentListAdp();
        mVDBinding.rvAppointments.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mAppointmentListAdp.bindToRecycleView(mVDBinding.rvAppointments);
        mAppointmentListAdp.setOnViewClick(car -> {
            ARouter.getInstance().build(ArouterPath.APPOINTMENT_ACTIVITY).withLong("id", car.getId()).navigation();
        });

        mVDBinding.srlAppointments.setOnLoadMoreListener(refreshLayout -> {
            mAppointmentListAdp.clear();
            mViewModel.appointments();
        });
        mVDBinding.srlAppointments.setEnableLoadMore(false);

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVDBinding.flSetting.getLayoutParams();
        layoutParams.topMargin = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.flSetting.requestLayout();
        mVDBinding.flSetting.setOnClickListener(v -> ARouter.getInstance().build(ArouterPath.SETTING_ACTIVITY).navigation());

    }

    @Override
    protected void initData() {
        mViewModel.userInfo();
        mViewModel.appointments();
        mViewModel.report();
    }

    @Override
    protected void visibleAgain() {
        initData();
    }

    @Override
    protected void initObservers() {
        super.initObservers();

        mViewModel.mldAppointmentsResponse.observe(this, carResponse -> {
            if (mVDBinding.srlAppointments.isRefreshing()) {
                mVDBinding.srlAppointments.finishRefresh();
            }
            if (carResponse != null) {
                mAppointmentListAdp.setNewData(carResponse.getData().getRecords());
            } else {
                mAppointmentListAdp.notifyDataSetChanged();
            }
        });

        mViewModel.mldReport.observe(this, report -> {
            String s = ComUtil.clearNoUseZero(report.getOrderSales()).toPlainString();
            mVDBinding.tvSaleMoney.setText(s);
            setSuitTvSize(mVDBinding.tvSaleMoney);
        });
    }

        private void setSuitTvSize(TextView textView) {

        float size = textView.getTextSize();
        int lines = TextViewUtil.getTextViewLines(textView, NUMBER_WIDTH_SPACE);

        while (lines > 1) {
            size = size - 0.5f;
            textView.setTextSize(size);
            lines = TextViewUtil.getTextViewLines(textView, NUMBER_WIDTH_SPACE);
            Loog.d("UserFragment", "size = " + size);

        }
    }

}
